<?php

namespace app\admin\controller;

use think\Db;
use think\Request;
use think\Validate;
use app\facade\Redis;

use app\models\{Banner, Exchange, Message, Annou, News, User as Users, Riderlist, IntergralBanner, Region,Config};

class System extends Base
{
    
    
    public function config()
    {
        $list = (new Config())->paginate(10);
        foreach ($list as $lists) {
            if ($lists['key_value'] == 'rate') {
                $lists['value'] = json_decode($lists['value'], true);
            }
        }
        return view(null, [
            'list' => $list
        ]);
    }


    public function config_edit()
    {
        if ($this->request->isPost()) {
            $data = $this->request->post();
            $validate = new Validate([
                'key_value' => 'require',
                'value' => 'require'
            ], [
                'key_value.require' => '名称不能为空',
                'value.require' => '路径不能为空',
            ]);
            if (!$validate->check($data)) {
                return json(['status' => 1, 'msg' =>  $validate->getError()]);
            }
            if ($data['key_value'] == 'ptdatas') {
                $data['value'] = json_encode($data['value'], true);
            }

            $res = (new Config())->allowfield(true)->save($data, ['key_value' => $data['key_value']]);
            if ($res) {
                return json(['status' => 0, 'msg' => '编辑成功']);
            }
            return json(['status' => 1, 'msg' => '编辑失败']);
        } else {
            $id = $this->request->get('key_value', '');
            $row = (new Config())->where('key_value', $id)->find();
            if (!empty($row) && $row['key_value'] == 'ptdatas') {
                $row['value'] = json_decode($row['value'], true);
            }
            return view('', [
                'row' => $row
            ]);
        }
    }

    public function exchange()
    {
        $model = new Exchange();
        $list = $model->order('id desc')->paginate(10, false, ['query' => $this->request->param()]);
        foreach ($list as $lists) {
            $area = (new Region())->where(['area_code' => $lists['area'], 'level' => 3])->find();
            $city = (new Region())->where(['area_code' => $lists['city'], 'level' => 2])->find();
            $province = (new Region())->where(['area_code' => $lists['province'], 'level' => 1])->find();
            $lists['area_text'] = $area['name'];
            $lists['city_text'] = $city['name'];
            $lists['province_text'] = $province['name'];
        }
        return view('', ['list' => $list]);
    }

    //添加修改
    public function exchange_add()
    {
        if ($this->request->isPost()) {
            $rdslist = Redis::get('clickreturn');
            if ($rdslist) {
                return json(['status' => 1, 'data' => [], 'msg' => '请' . $rdslist . 's后再试']);
            }
            Redis::setex('clickreturn', 2, 1);
            $data = input('post.');
            $model = new Exchange();
            if (empty($data['province']) || empty($data['city']) || empty($data['area']) || empty($data['scale'])) {
                return json(['status' => 1, 'data' => [], 'msg' => '区域、兑换参数不能为空']);
            }
            if ($data['scale'] <= 0) {
                return json(['status' => 1, 'data' => [], 'msg' => '兑换参数不能为负数']);
            }
            $exchangehave = $model->where('area', $data['area'])->find();
            if (!empty($exchangehave) && empty($data['id'])) {
                return json(['status' => 1, 'data' => [], 'msg' => '区域价格已设置']);
            }
            if (!empty($exchangehave) && !empty($data['id']) && $data['id'] != $exchangehave['id']) {
                return json(['status' => 1, 'data' => [], 'msg' => '区域价格已设置']);
            }
            if (!empty($data['id'])) {
                $result = $model->save($data, ['id' => $data['id']]);
            } else {
                $result = $model->save($data);
            }
            if ($result) {
                return json(['status' => 0, 'msg' => '操作成功']);
            } else {
                return json(['status' => 1, 'msg' => '操作失败,请重试']);
            }
        } else {
            $id = $this->request->get('id');
            $row = [];
            $province = [];
            $city = [];
            $area = [];
            $province = (new Region())->where(['level' => 1])->select();
            if ($id > 0) {
                $row = (new Exchange())->where('id', $id)->find();
                $city = (new Region())->where(['level' => 2, 'area_code' => $row['city']])->select();
                $area = (new Region())->where(['level' => 3, 'area_code' => $row['area']])->select();
            }
            return view('', ['row' => $row, 'province' => $province, 'city' => $city, 'area' => $area]);
        }
    }

    public function region()
    {
        $model = new Region();
        $data = $this->request->get();
        if (!empty($data['street'])) {
            $model = $model->where('id', $data['street']);
        } elseif (!empty($data['area'])) {
            $model = $model->where('area_code', $data['area']);
        } elseif (!empty($data['city'])) {
            $model = $model->where('area_code', $data['city']);
        } elseif (!empty($data['province'])) {
            $model = $model->where('area_code', $data['province']);
        } elseif (!empty($data['id'])) {
            $model = $model->where('parent_id', $data['id']);
        }
        $level = 1;
        if (!empty($data['level'])) {
            $level = $data['level'];
        }
        if (!empty($data['parent_id'])) {
            $parent_id = $data['parent_id'];
        }
        $list = $model->where('level', $level)->order('id desc')->paginate(20, false, ['query' => $this->request->param()]);
        return view('', ['list' => $list, 'parent_id' => $parent_id ?? 0]);
    }

    //添加修改
    public function region_add()
    {
        if ($this->request->isPost()) {
            $rdslist = Redis::get('clickreturn');
            if ($rdslist) {
                return json(['status' => 1, 'data' => [], 'msg' => '请' . $rdslist . 's后再试']);
            }
            Redis::setex('clickreturn', 2, 1);
            $data = input('post.');
            $result0 = true;
            if (!empty($data['id'])) {
                $region = (new Region())->where('id', $data['id'])->find();
                if (empty($region)) {
                    return json(['status' => 1, 'msg' => '编码信息不存在，请重试']);
                }
                //判断编码是变更
                if ($data['area_code'] != $region['area_code'] && $region['level'] < 4) {
                    //变更后查询编码信息的重复信
                    $haveregion = (new Region())->where('area_code', $data['area_code'])->find();
                    if (!empty($haveregion) && $haveregion['level'] < 4) {
                        return json(['status' => 1, 'msg' => '编码信息已重复']);
                    }
                }
                //判断编码是变更
                if ($data['area_code'] != $region['area_code'] && $region['level'] == 3) {
                    //变更后变更街道下级信息
                    $result0 = (new Region())->where(['parent_id' => $region['id'], 'level' => 4])->update(['area_code' => $data['area_code']]);
                }
                $result = (new Region())->allowField(true)->save($data, ['id' => $data['id']]);
            } else {
                $data['level'] = 1;
                $data['whole_name'] = $data['name'];
                if ($data['parent_id'] > 0) {
                    $parent_id = (new Region())->where('id', $data['parent_id'])->find();
                    if (empty($parent_id)) {
                        return json(['status' => 1, 'msg' => '绑定上级信息错误']);
                    }
                    $data['level'] = $parent_id['level'] + 1;
                    $data['whole_name'] = $parent_id['whole_name'] . ',' . $data['name'];
                    if ($parent_id['level'] == 3) {
                        $data['area_code'] = $parent_id['area_code'];
                    }
                }
                $haveregion = (new Region())->where(['area_code' => $data['area_code'], 'level' => $data['level']])->find();
                if (!empty($haveregion) && $haveregion['level'] < 4) {
                    return json(['status' => 1, 'msg' => '编码信息已重复']);
                }
                $result = (new Region())->allowField(true)->save($data);
            }
            if ($result && $result0) {
                return json(['status' => 0, 'msg' => '操作成功']);
            } else {
                return json(['status' => 1, 'msg' => '操作失败,请重试']);
            }
        } else {
            $id = $this->request->get('id');
            $row = [];
            $row['parent_id'] = $this->request->get('parent_id/d', 0);
            if ($id > 0) {
                $row = (new Region())->where('id', $id)->find();
            }
            return view('', ['row' => $row]);
        }
    }


    public function region_del()
    {
        $id = trim(input('id'), ',');
        $result = (new Region())->where('id', $id)->delete();
        if ($result) {
            return json(['status' => 0, 'msg' => '操作成功']);
        } else {
            return json(['status' => 1, 'msg' => '删除失败']);
        }
    }

    public function banner()
    {
        $ModelFun = new Banner();
        $data = $this->request->get();
        $list = $ModelFun->order('id desc')->paginate(20, false, ['query' => $this->request->param()]);
        return view('', ['list' => $list]);
    }

    //添加修改
    public function banner_add()
    {
        if ($this->request->isPost()) {
            $rdslist = Redis::get('clickreturn');
            if ($rdslist) {
                return json(['status' => 1, 'data' => [], 'msg' => '请' . $rdslist . 's后再试']);
            }
            Redis::setex('clickreturn', 2, 1);
            $data = input('post.');
            if (empty($data['id'])) {
                $result = (new Banner())->allowField(true)->save($data);
            } else {
                $result = (new Banner())->allowField(true)->save($data, ['id' => $data['id']]);
            }
            if ($result) {
                return json(['status' => 0, 'msg' => '操作成功']);
            } else {
                return json(['status' => 1, 'msg' => '操作失败']);
            }
        } else {
            $id = $this->request->get('id');
            $row = (new Banner())->where('id', $id)->find();
            return view('', ['row' => $row]);
        }
    }

    public function banner_del()
    {
        $id = trim(input('id'), ',');
        $result = (new Banner())->where('id', $id)->delete();
        if ($result) {
            return json(['status' => 0, 'msg' => '操作成功']);
        } else {
            return json(['status' => 1, 'msg' => '删除失败']);
        }
    }

    public function annou()
    {
        $ModelFun = new Annou();
        $data = $this->request->get();
        $list = $ModelFun->order('id desc');
        if (!empty($data['type'])) {
            $list = $list->where('type', $data['type']);
        }
        if (!empty($data['title'])) {
            $list = $list->where('title', 'like', '%' . $data['title'] . '%');
        }
        $list = $list->paginate(20, false, ['query' => $this->request->param()]);
        return view('', ['list' => $list]);
    }

    //添加修改
    public function annou_add()
    {
        if ($this->request->isPost()) {
            $rdslist = Redis::get('clickreturn');
            if ($rdslist) {
                return json(['status' => 1, 'data' => [], 'msg' => '请' . $rdslist . 's后再试']);
            }
            Redis::setex('clickreturn', 2, 1);
            $data = input('post.');
            if (empty($data['id'])) {
                $result = (new Annou())->allowField(true)->save($data);
            } else {
                $result = (new Annou())->allowField(true)->save($data, ['id' => $data['id']]);
            }
            if ($result) {
                return json(['status' => 0, 'msg' => '操作成功']);
            } else {
                return json(['status' => 1, 'msg' => '操作失败']);
            }
        } else {
            $id = $this->request->get('id');
            $row = (new Annou())->where('id', $id)->find();
            return view('', ['row' => $row]);
        }
    }

    public function annou_del()
    {
        $id = trim(input('id'), ',');
        $result = (new Annou())->where('id', $id)->delete();
        if ($result) {
            return json(['status' => 0, 'msg' => '操作成功']);
        } else {
            return json(['status' => 1, 'msg' => '删除失败']);
        }
    }



    public function message()
    {
        $ModelFun = new Message();
        $data = $this->request->get();
        $list = $ModelFun->order('id desc');
        if (isset($data['status']) && $data['status']) {
            $list = $list->where('status', $data['status']);
        }
        $list = $list->paginate(20, false, ['query' => $this->request->param()]);
        return view('', ['list' => $list]);
    }

    public function message_add()
    {
        $rdslist = Redis::get('clickreturn');
        if ($rdslist) {
            return json(['code' => 1, 'data' => [], 'msg' => '请' . $rdslist . 's后再试']);
        }
        Redis::setex('clickreturn', 2, 1);
        $ModelFun = new Message();
        $data = $this->request->post();
        if (empty($data['id']) || $data['id'] == 0) {
            return json(['status' => 1, 'msg' => 'ID不能为空']);
        }
        if (empty($data['reply'])) {
            return json(['status' => 1, 'msg' => '回复内容不能为空']);
        }
        $list = $ModelFun->where('id', $data['id'])->find();
        if (empty($list)) {
            return json(['status' => 1, 'msg' => '反馈信息不存在']);
        }
        $res = $ModelFun->where('id', $data['id'])->update(['status' => 2, 'reply' => $data['reply']]);
        if ($res) {
            return json(['status' => 0, 'msg' => '操作成功']);
        } else {
            return json(['status' => 1, 'msg' => '操作失败']);
        }
    }

    public function news()
    {
        $ModelFun = new News();
        $data = $this->request->get();
        $list = $ModelFun->order('id desc');
        if (!empty($data['type'])) {
            $list = $list->where('type', $data['type']);
        }
        if (!empty($data['title'])) {
            $list = $list->where('title', 'like', '%' . $data['title'] . '%');
        }
        $list = $list->paginate(20, false, ['query' => $this->request->param()]);
        return view('', ['list' => $list]);
    }

    //添加修改
    public function news_add()
    {
        if ($this->request->isPost()) {
            $rdslist = Redis::get('clickreturn');
            if ($rdslist) {
                return json(['status' => 1, 'data' => [], 'msg' => '请' . $rdslist . 's后再试']);
            }
            Redis::setex('clickreturn', 2, 1);
            $data = input('post.');
            if (empty($data['type'])) {
                return json(['status' => 1, 'msg' => '类型不能为空']);
            }
            if (empty($data['id'])) {
                if ($data['type'] != 4) {
                    $newcount = (new News())->where('type', $data['type'])->count();
                    if ($newcount > 0) {
                        return json(['status' => 1, 'msg' => '该类型文本只能传一篇']);
                    }
                }
                $result = (new News())->allowField(true)->save($data);
            } else {
                $result = (new News())->allowField(true)->save($data, ['id' => $data['id']]);
            }
            if ($result) {
                return json(['status' => 0, 'msg' => '操作成功']);
            } else {
                return json(['status' => 1, 'msg' => '操作失败']);
            }
        } else {
            $id = $this->request->get('id');
            $row = (new News())->where('id', $id)->find();
            return view('', ['row' => $row]);
        }
    }

    public function news_del()
    {
        $id = trim(input('id'), ',');
        $result = (new News())->where('id', $id)->delete();
        if ($result) {
            return json(['status' => 0, 'msg' => '操作成功']);
        } else {
            return json(['status' => 1, 'msg' => '删除失败']);
        }
    }
}
